package com.zretc.support.controller;

import com.zretc.support.entity.Course;
import com.zretc.support.entity.Message;
import com.zretc.support.service.JoincourseService;
import com.zretc.support.service.MessageService;
import com.zretc.support.service.serviceimpl.CourseServiceImpl;
import com.zretc.support.service.serviceimpl.MessageServiceImpl;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api")
public class MessageController {
    @Autowired
    MessageServiceImpl msi;
    @Autowired
    CourseServiceImpl csi;
    @Autowired
    JoincourseService joincourseService;
    @Autowired
    PageData pageData;

    /**
     * 分页+模糊
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getMes",produces = "application/json;charset=utf-8")
    public PageData<Message> getMesByPage(@RequestParam(value = "keywords",defaultValue = "") String keywords,@RequestParam(value = "page",defaultValue = "1") int page,@RequestParam(value = "limit",defaultValue = "6") int limit){
        Page<Message> pd=msi.selectByPage(keywords,page,limit);
        pageData.setRecords(pd.getContent());
        pageData.setCode(0);
        pageData.setTotal((int) pd.getTotalElements());
        pageData.setSize(pd.getSize());
        pageData.setCurrent(pd.getNumber()+1);
        pageData.setMsg("");
        System.out.println(pd.getContent());
        return pageData;
    }

    /**
     * 分页+时间段
     * @param time1
     * @param time2
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getMesByTime",produces = "application/json;charset=utf-8")
    public PageData<Message> getMesByTime(@RequestParam(value = "time1") String time1,@RequestParam(value = "time2") String time2,@RequestParam(value = "page",defaultValue = "1") int page,@RequestParam(value = "limit",defaultValue = "6") int limit){
        time1 = time1.replace("Z", " UTC");
        time2 = time2.replace("Z", " UTC");
        SimpleDateFormat simple=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        Date tim1=null;
        Date tim2=null;
        try {
             tim1=simple.parse(time1);
             tim2=simple.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Page<Message> pd=msi.selectByDate(tim1,tim2,page,limit);
        pageData.setRecords(pd.getContent());
        pageData.setCode(0);
        pageData.setTotal((int) pd.getTotalElements());
        pageData.setSize(pd.getSize());
        pageData.setCurrent(pd.getNumber()+1);
        pageData.setMsg("");
        return pageData;
    }

    /**
     * 增加消息
     * @param message
     * @return
     */
    @RequestMapping(value="addMes",method = RequestMethod.PUT)
    public AjaxResponse addMes(@RequestBody Message message){
        message.setMessageDate(new Date());
        String msg="添加成功！";
        int code=200;
        int n=msi.addMessage(message);
        if (n==0){
            msg="添加失败！";
            code=500;
        }
        AjaxResponse ajaxResponse=new AjaxResponse(msg,code);
        return ajaxResponse;
    }

    @RequestMapping("getusermes/{userId}")
    public PageData getUserMes(@PathVariable("userId") String userId,int page,int size){

       Page<Message> pd = msi.selMesByUserId(userId, page, size);

       pageData.setRecords(pd.getContent());
       pageData.setCode(0);
       pageData.setTotal((int) pd.getTotalElements());
       pageData.setSize(size);
       pageData.setCurrent(page);
       pageData.setMsg("");

       return pageData;
    }



}
